package view;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Scanner;
import java.util.regex.Pattern;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import controller.AnalyserCorpus;
import metier.Filtre;

public class Frame extends JFrame{
	
	JLabel path;
	String pathCorpus;
	JButton parcourir,classer,matrice,decoupage,fabriquer_filtre,valider_corpus;

	public Frame() {
		super();
        init();
        // GRIDLAYOUT
        setLayout(new GridLayout(5,1));
        add(TopPanel());
        
        classer = new JButton("classer");
        matrice = new JButton("matrice");
        decoupage = new JButton("decoupage");
        fabriquer_filtre = new JButton("Fabriquer des filtres");
        
        add(classer);
        /*classer.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				FClasser fc = new FClasser();
			}});*/
        classer.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				JClasser classer = new JClasser();
				
			}
		});
        add(matrice);
        matrice.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				JMatriceDocMot jMatrice = new JMatriceDocMot();
			}});
        add(decoupage);
        decoupage.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				JDecoupage jDecoupe = new JDecoupage();
				
			}
		});
        add(fabriquer_filtre);
        fabriquer_filtre.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				FFiltre fFiltre = new FFiltre();				
			}
		});
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
	
	}
	
	public void init() {
		setTitle("Fouille De Textes - 2015");
	    setSize(new Dimension(470, 400));
	    setLocationRelativeTo(null);
	}
	
	public JPanel TopPanel() {
		JPanel res = new JPanel();
		res.setLayout(new GridLayout(1,3));
		path = new JLabel("/path");
		res.add(path);
		parcourir = new JButton("parcourir");
		res.add(parcourir);
		parcourir.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser openFile = new JFileChooser();
				openFile.setAcceptAllFileFilterUsed(false);
                openFile.showOpenDialog(null);
                
                try {
                    // Open an input stream
                    Scanner reader = new Scanner(openFile.getSelectedFile());
                    if(reader != null) {
                    	String separatorPath = File.separator;
                		String[] tab = openFile.getSelectedFile().toString().split(Pattern
                				.quote(separatorPath));
                        path.setText(tab[tab.length-1]);
                        pathCorpus = openFile.getSelectedFile().toString();
                        
                        System.out.println(pathCorpus);
                        
                        
                    }
                }
                 catch( Exception e1){
                	 System.out.println(e1);
                 }
				
                }});
		valider_corpus = new JButton("Valider Corpus");
		res.add(valider_corpus);
		
		valider_corpus.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				AnalyserCorpus.analyserCorpusController(pathCorpus);
			}
		});
		
		return res;
	}
	
	public static void main(String...args){
		Frame main = new Frame();
	}
}
